<?php
class material extends CI_Controller {

    function __construct() {
        parent::__construct();
        $level_id = $this->session->userdata('logged_in');
        if ($level_id['level_id'] == null) {
            redirect('user/login', 'refresh');
        }
        if ($level_id['level_id'] == 0) {
            redirect('user', 'refresh');
        }
    }
	
	function material_list(){
		$crud = new grocery_CRUD();
		
		$crud->set_table('material');
		$crud->set_relation('material_type_id', 'material_type', 'material_type_name',array('deleted' => 'No'));
		$crud->where('material.deleted','No');
		$crud->columns('material_code', 'material_name', 'material_type_id', 'material_qty', 'material_uom', 'material_buy_price', 'material_rent_price', 'material_desc');
		 $crud->required_fields('material_code', 'material_name', 'material_type_id', 'material_qty', 'material_uom', 'material_buy_price', 'material_rent_price');

        $crud->display_as('material_code', 'Material Code');
        $crud->display_as('material_name', 'Material Name');
        $crud->display_as('material_type_id', 'Material Type Name');
        $crud->display_as('material_qty', 'Quantity');
        $crud->display_as('material_uom', 'Unit Of Measure');
        $crud->display_as('material_buy_price', 'Buy Price');
        $crud->display_as('material_rent_price', 'Rent Price');
		$crud->display_as('material_desc', 'Description');
		
		$crud->unset_add_fields('material_id', 'last_modifier', 'last_modified', 'deleted');
		$crud->unset_edit_fields('material_id', 'last_modifier', 'last_modified', 'deleted');
		
		$crud->callback_column('material_buy_price', array($this, 'formatRupiah'));
		$crud->callback_column('material_rent_price', array($this, 'formatRupiah'));
		$crud->callback_column('material_qty', array($this, 'point_to_comma'));
		$crud->callback_field('material_buy_price', array($this, 'formatBuyField'));
		$crud->callback_field('material_rent_price', array($this, 'formatRentField'));
		$crud->callback_field('material_qty', array($this, 'formatQtyField'));
		$crud->callback_after_update(array($this, 'after_update_materiallist'));
		$crud->callback_delete(array($this, 'after_delete_materiallist'));
		$crud->callback_after_insert(array($this, 'after_insert_materiallist'));
		$crud->callback_add_field('material_code',array($this,'field_code_materiallist'));
		$crud->callback_before_insert(array($this, 'cb_before_material'));
		$crud->callback_before_update(array($this, 'cb_before_material'));
        
		$crud->set_subject('Material List');

        $output = (array) $crud->render();
        $output['title'] = 'Material List';

        $this->load->template('room/materiallist', $output);
	}
	
	function material_type(){
		$crud = new grocery_CRUD();
		
		$crud->set_table('material_type');
		$crud->required_fields('material_type_code', 'material_type_name');
		$crud->columns('material_type_code', 'material_type_name', 'material_type_desc');
		$crud->where('deleted','No');
		
		$crud->display_as('material_type_code',' Material Code');
		$crud->display_as('material_name',' Material Name');
		$crud->display_as('material_desc',' Description');
		$crud->display_as('material_qty', 'Quantity');
		$crud->display_as('material_uom', 'Unit Of Measure');
		$crud->display_as('material_buy_price', 'Buy Price');
		$crud->display_as('material_rent_price', 'Rent Price');
		
		$crud->callback_after_update(array($this, 'after_update_materialtype'));
		$crud->callback_delete(array($this, 'after_delete_materialtype'));
		$crud->callback_after_insert(array($this, 'after_insert_materialtype'));
		$crud->callback_add_field('material_type_code',array($this,'field_code_materialtype'));
		
		$crud->unset_add_fields('material_type_id', 'last_modifier', 'last_modified', 'deleted');
		$crud->unset_edit_fields('material_type_id', 'last_modifier', 'last_modified', 'deleted');
		
		$crud->set_subject('Material Type');

        $output = (array) $crud->render();
        $output['title'] = 'Material Type';

        $this->load->template('room/materialtype', $output);
	}
	
	function after_insert_materialtype($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material_type', array('last_modifier'=>$level_id['user_id']), array('material_type_id'=>$primary_key));
	}
	function after_update_materialtype($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material_type', array('last_modifier'=>$level_id['user_id']),array('material_type_id'=>$primary_key));
	}
	function after_delete_materialtype($primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material_type', array('last_modifier'=>$level_id['user_id'], 'deleted'=>'Yes'), array('material_type_id'=>$primary_key));
		$this->db->update('material', array('last_modifier'=>$level_id['user_id'], 'deleted'=>'Yes'), array('material_type_id'=>$primary_key));
	}
	function field_code_materialtype(){
		$query = "select max(material_type_code) as max from material_type";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max+1;
		return '<input type="text" maxlength="4" value="'.$string.'" name="material_type_code" style="width:auto">';
	}
	
	
	function after_insert_materiallist($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material', array('last_modifier'=>$level_id['user_id']), array('material_id'=>$primary_key));
	}
	function after_update_materiallist($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material', array('last_modifier'=>$level_id['user_id']),array('material_id'=>$primary_key));
	}
	
	function after_delete_materiallist($primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('material', array('deleted'=>'Yes', 'last_modifier'=>$level_id['user_id']),array('material_id'=>$primary_key));
	}
	
	function field_code_materiallist(){
		$query = "select max(material_code) as max from material";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max+1;
		return '<input type="text" maxlength="4" value="'.$string.'" name="material_code" style="width:auto">';
	}
	
	function formatRupiah($value) {
		if($value != null) {
			return 'Rp  ' . number_format($value, 2, ',', '.');
		}
		else {
			return $value;
		}
    }
	
	function point_to_comma($number) {
		return $this->load->point_to_comma($number);
	}
	
	function formatQtyField($value = '') {
        return '<input class="qty" type="text" value="' . $value . '" name="material_qty" style="width:auto">';
    }
	
	function formatBuyField($value = '') {
        return 'Rp <input class="rp" type="text" value="' . $value . '" name="material_buy_price" style="width:auto">';
    }
	
	function formatRentField($value = '') {
        return 'Rp <input class="rp" type="text" value="' . $value . '" name="material_rent_price" style="width:auto">';
    }
	
	function cb_before_material($post) {
		$post['material_qty'] = $this->load->comma_to_point($post['material_qty']);
		$post['material_buy_price'] = $this->load->comma_to_point($post['material_buy_price']);
		$post['material_rent_price'] = $this->load->comma_to_point($post['material_rent_price']);
		
		return $post;
	}
}
?>